package com.halachawiki.shared;

import java.io.Serializable;

public class HalachicPeriods implements Serializable {

	private static final long serialVersionUID = 1L;

	// careful when modifying enum, changes binary compat with database contents.
	public enum Period {
		TANAIM,
		AMORAIM,
		GEONIM,
		RISHONIM,
		AHRONIM
	};
	private Period period;
	private String displayString;
	private int index;
	
	public Period getPeriod() { return period; }
	public String getDisplayString() { return displayString; }
	public int getIndex() { return index; }
	
	public final static HalachicPeriods TANAIM = new HalachicPeriods("תנאים", Period.TANAIM, 1);
	public static final HalachicPeriods AMORAIM = new HalachicPeriods("אמוראים", Period.AMORAIM, 2);
	public static final HalachicPeriods GEONIM = new HalachicPeriods("גאונים", Period.GEONIM, 3);
	public static final HalachicPeriods RISHONIM = new HalachicPeriods("ראשונים", Period.RISHONIM, 4);
	public static final HalachicPeriods AHRONIM = new HalachicPeriods("אחרונים", Period.AHRONIM, 5);
	
	public static final HalachicPeriods allPeriods[] = { TANAIM, AMORAIM, GEONIM, RISHONIM, AHRONIM };
	
	public static HalachicPeriods getPeriod(String displayString) {
		if (displayString == null) { return null; }
		for (HalachicPeriods period : allPeriods)
			if (period.getDisplayString().equals(displayString))
				return period;
		return null; // should not happen
	}

	public static HalachicPeriods getPeriod(Period period) {
		if (period == null) { return null; }
		for (HalachicPeriods candidatePeriod : allPeriods)
			if (candidatePeriod.getPeriod().equals(period))
				return candidatePeriod;
		return null; // should not happen
	}

	public static HalachicPeriods getPeriod(int index) {
		for (HalachicPeriods candidatePeriod : allPeriods)
			if (candidatePeriod.getIndex()==index)
				return candidatePeriod;
		return null; // should not happen
	}

	private HalachicPeriods(String displayString, Period period, int index) { 
		this.period = period;
		this.displayString = displayString;
		this.index = index;
	}
	private HalachicPeriods() { } // needed for serialization
	
}
